package com.agu.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by agu on 16/11/2.
 */
public class FiledUtil {

    public static String toTableName(String name)
    {
        String[] temp = name.split("_");

        StringBuilder sb = new StringBuilder();

        for(String str:temp)
        {
            int n = str.length();
            String t="";
            if (n > 1)
            {

                t = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase() ;
            }
            else
            {
                t = str.toUpperCase();
            }
            sb.append(t);
        }

        return sb.toString();
    }

    public static String toMethodName(String name)
    {
        String[] temp = name.split("_");

        StringBuilder sb = new StringBuilder();

        for (String str:temp)
        {
            int n = str.length();
            String t = "";
            if (n > 1)
            {

                t = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
            }
            else
            {
                t = str.toUpperCase();
            }
            sb.append(t);
        }

        return sb.toString();
    }

    public static String toMethodName2(String name)
    {
        StringBuilder sb = new StringBuilder();
        int n = name.length();
        String t = "";
        if (n > 1)
        {

            t = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
        }
        else
        {
            t = name.toUpperCase();
        }
        sb.append(t);
        return sb.toString();
    }

    /*
        首字母大写
     */
    public static String toF1BigName(String name)
    {
        StringBuilder sb = new StringBuilder();
        int n = name.length();
        String t = "";
        if (n > 1)
        {

            t = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        else
        {
            t = name.toUpperCase();
        }
        sb.append(t);
        return sb.toString();
    }

    /*
      首字母小写
   */
    public static String toF1SmallName(String name)
    {
        StringBuilder sb = new StringBuilder();
        int n = name.length();
        String t = "";
        if (n > 1)
        {

            t = name.substring(0, 1).toLowerCase() + name.substring(1);
        }
        else
        {
            t = name.toUpperCase();
        }
        sb.append(t);
        return sb.toString();
    }

    public static String toFieldName(String name)
    {
        String[] temp = name.split("_");

        StringBuilder sb = new StringBuilder();
        int i=0;
        for(String str:temp)
        {
            int n = str.length();
            String t = "";
            i++;
            if (i == 1)
            {
                t = str.toLowerCase();
            }
            else
            {
                if (n > 1)
                {
                    t = str.substring(0, 1).toUpperCase() +str.substring(1).toLowerCase();
                }else{
                    t = str.toUpperCase();
                }
            }
            sb.append(t);
        }
        return sb.toString();
    }


    public static Map strToMap(String str)
    {
        try {
        String json = "{"+str+"}";

        json =   json.replaceAll("'","\"");

        Map map  = JsonUtils.toObject(json, HashMap.class);

        return map;
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }

        return  null;
    }
}